<template>
    <div id="essay">
        <!--工具栏目-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
            <el-form :inline="true" :model="ActData">
                <el-form-item>
                    <el-input type="number" min="0" v-model="ActData.id" placeholder="请输入要搜索的ID"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="ActData.title" placeholder="请输入要搜索的标题"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="postEssay">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--TAble-->
        <el-table :data="ActIty" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
                  style="width: 100%;">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="ID" width="80"></el-table-column>
            <el-table-column prop="title" label="标题"></el-table-column>
            <el-table-column prop="type" label="类型"></el-table-column>
            <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                    <el-button size="small"  @click="handleConnect(scope.$index, scope.row)">打开连接</el-button>
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <!--<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>-->
                </template>
            </el-table-column>
        </el-table>
        <!--分页-->
        <el-col :span="24" class="toolbar">
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="size"
                           :total="total" style="float:right;"></el-pagination>
        </el-col>

        <!--新增-->
        <el-dialog title="添加活动" v-model="addFormVisible" :close-on-click-modal="false" top="5%" size="large">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="标题" prop="title">
                    <el-input v-model="addForm.title" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-input v-model="addForm.type" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <UEditor @ready="editorReady" style=""></UEditor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="addSubmit('addForm')" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--编辑-->
        <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false" top="5%" size="large">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="标题" prop="title">
                    <el-input v-model="editForm.title" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-input v-model="editForm.type" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <UEditor @ready="editorReadyEdit" style=""></UEditor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="editSubmit('editForm')" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import util from '../../common/js/util';
    import {essayAdd,essayDel,essayList,essayListS,essayUpdate} from '../../api/api';
    import UEditor from '../../components/ueditor/index';
    export default {
        data() {
            return {
                ActIty: [],
                total: 0,
                page: 1,
                size: 15,
                ActData: {
                    id: null,
                    title: ''
                },
                listLoading: false,
                /*新增界面*/
                addFormVisible: false,
                addLoading: false,
                addFormRules: {
                    title: {required: true, message: '请输入标题', trigger: 'blur'},
                    type: {required: true, message: '请输入类型', trigger: 'blur'},
                },
                /*新增界面数据*/
                addForm: {
                    title: '',
                    type: ''
                },
                ueAdd: null,
                /*编辑*/
                editFormVisible: false,
                editLoading: false,
                editFormRules: {
                    title: {required: true, message: '请输入标题', trigger: 'blur'},
                },
                ueEdit: null,
                //编辑界面数据
                editForm: {
                    id: null,
                    title: '',
                    type: ''
                },
            }
        },
        methods: {
            editorReady (editorInstance) {
                this.ueAdd = editorInstance;
                editorInstance.setContent('');
                editorInstance.addListener('contentChange', () => {
                    console.log('内容发生了变化：', editorInstance.getContent());
                });
            },
            editorReadyEdit (editorInstance) {
                this.ueEdit = editorInstance;
                editorInstance.setContent('');
                editorInstance.addListener('contentChange', () => {
                    console.log('内容发生了变化：', editorInstance.getContent());
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            handleCurrentChange(val) {
                this.page = val;
                this.postEssay();
            },
            postEssay(){
                let para = {
                    page: this.page,
                    size: this.size,
                    data: this.ActData
                };
                this.listLoading = true;
                essayList(para).then((res) => {
                    console.log(res);
                    this.ActIty = res.data.content;
                    this.total = res.data.totalElements;
                    this.listLoading = false;
                });
            },
            /*新增*/
            handleAdd(){
                this.addFormVisible = true;
                this.addForm = {
                    title: '',
                    type: ''
                };
                if(this.ueAdd !== null){
                    this.ueAdd.setContent('');
                }
            },
            addSubmit(a){
                if(this.ueAdd.getContent()===''){this.$message({message: '请填写内容', type: 'error'});return;}
                this.$refs[a].validate((valid) => {
                    if (valid){
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = this.addForm;
                            para.body=this.ueAdd.getContent();
                            essayAdd(para).then((res) => {
                                this.addLoading = false;
                                this.$message({message: res.message, type: 'success'});
                                this.addFormVisible = false;
                                this.postEssay();
                            });
                        });
                    }
                });
            },

            /*编辑*/
            /*显示编辑界面*/
            handleEdit(index, row) {
                console.log(row);
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
                var time = setInterval(() => {
                    if(this.ueEdit!==null){
                        this.ueEdit.setContent(row.body);
                        clearInterval(time);
                    }
                },100)
            },
            /*打开连接*/
            handleConnect(index,row){
                window.open(this.$store.state.imagesUrl+'wap/app/article/info/'+row.type);
            },
            editSubmit(a) {
                if(this.ueEdit.getContent()===''){this.$message({message: '请填写内容', type: 'error'});return;}
                this.$refs[a].validate((valid) => {
                    if(valid){
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editFormVisible = true;
                            let para = this.editForm;
                            para.body=this.ueEdit.getContent();
                            essayUpdate(para).then((res) => {
                                this.editLoading = false;
                                this.$message({message: res.message, type: 'success'});
                                this.editFormVisible = false;
                                this.postEssay();
                            });
                        });
                    }
                });
            },
        },
        mounted(){
            this.postEssay();
        },
        components:{
            UEditor
        }
    }
</script>
<style type="text/scss" lang="scss" scoped>
    #essay{
    }
</style>